package org.example.likou150.stack;


import java.util.Stack;


// https://leetcode.cn/problems/valid-parentheses/?envType=study-plan-v2&envId=top-interview-150
public class IsValid {
    public static void main(String[] args) {
        IsValid isValid=new IsValid();
        boolean valid = isValid.isValid("([])");
        System.out.println(valid);
    }

    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        char[] charArray = s.toCharArray();
        for (int i=charArray.length-1;i>=0;i--) {
            char c = charArray[i];
            if(stack.empty()){
                stack.push(c);
                continue;
            }
            Character peek = stack.peek();
            if(c=='(' && peek==')'){
                stack.pop();
            } else if(c=='[' && peek==']'){
                stack.pop();
            }else if(c=='{' && peek=='}'){
                stack.pop();
            }else {
                stack.push(c);
            }
        }
        return stack.empty();
    }
}
